Skip to content

fix(claim): send X-Device-Mid from the env fallback (Android claim always failed with biz 3001) - #84

Merged
TriDefender merged 1 commit into
TriDefender:masterfrom
Baidaofu:fix/claim-device-mid
Nov 17, 2026
Merged

TriDefender merged 1 commit into
TriDefender:masterfrom
Baidaofu:fix/claim-device-mid

Conversation

@Baidaofu

Copy link
Copy Markdown
Contributor

Symptom

On the Android app (and any setup where identity.deviceMid is not in
config.yaml), the auto-claim scheduler logs this every cooldown cycle and
never claims anything:

[claim] claim: invalid_request (3001) — parameter error; retry in 600s

The desktop/CLI path claims fine on the same account, and GET /api/v1/zcode-plan/billing/preview still enumerated plans — so it looked like the campaign rejected the account rather than the request being malformed.

Root cause

Two gaps that combine:

  1. src/proxy/identity.ts reads X-Device-Mid as env (ZCODE_IDENTITY_DEVICE_MID) > YAML, and the type docs on ProxyIdentity say the same ("Android: injected via the env var — env wins over YAML"). resolveIdentity() in src/config/loader.ts only read YAML, so config.identity.deviceMid was undefined on Android (NodeRunner deliberately never writes it to config.yaml).
  2. startAutoClaim()'s billing client took deviceMid: config.identity.deviceMid with no fallback, while marketingDeviceMid fell back to stableMarketingDeviceMid(). Preview therefore worked through the marketing face, and the claim POST went out with X-Device-Mid omitted.

Per the empirical note already in src/claim/client.ts (0828 / wk-0918 campaigns), the gateway answers biz 3001 "parameter error" to billing calls whose X-Device-Mid is missing or not a UUID — exactly the classification classifyClaimCode() produced.

Fix

  • src/config/loader.ts: resolve identity.deviceMid as env > YAML (blank counts as unset), matching the documented contract and the header-building order, so both planes share one fingerprint.
  • src/claim/runtime.ts: new claimDeviceMid() used for both the preview and claim clients — the billing face has no fallback of its own, so a config without identity.deviceMid must never reach it with the header dropped.
  • Tests: env precedence + blank-env fallthrough in loader.test.ts; new claim/runtime.test.ts pinning the shared stable-UUID contract.

Verification

  • bun test: 1229 pass / 0 fail (66 → now 69 files, includes the new suite).

  • Live on the Android app (endpoint zcode.z.ai, plan zai/start-plan, config.yaml with no identity block): before the patch the scheduler produced the 3001 line every 10 min; after it the very next tick logged

    [claim] claim: claimed plan zcode-v3-start-plan-1117 (activates 2026-11-17T11:48:00.000Z)
    

    and the plan shows up in the official client for the same account.

No behaviour change for desktop/CLI users: ensureDeviceMidInConfig() keeps the YAML value and nothing else reads this field.

Android injects the per-install device id as ZCODE_IDENTITY_DEVICE_MID and
never writes identity.deviceMid into config.yaml, but the loader only read
YAML — so the claim client sent no X-Device-Mid and the gateway answered biz
3001 "parameter error" on every claim POST. Preview survived only because the
marketing face has its own derived fallback, which is why the app could
enumerate plans and then fail to grab them. Observed on the Android app as
`[claim] claim: invalid_request (3001) — parameter error` every 10 min while
desktop/CLI claimed fine on the same account.

- loader: resolve identity.deviceMid as env > YAML (blank counts as unset),
  matching the documented contract on ProxyIdentity and the order
  proxy/identity.ts already applies to the request headers, so the two planes
  agree on one fingerprint.
- claim/runtime: add claimDeviceMid() and use it for BOTH the preview and
  claim clients — the billing face has no fallback of its own, so a config
  without identity.deviceMid must not reach it with the header omitted.
- tests: env precedence / blank-env fallthrough in loader.test.ts, plus
  claim/runtime.test.ts for the shared-UUID contract.
@TriDefender
TriDefender merged commit a1d77b1 into TriDefender:master Nov 17, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants